package SpaceIT.pages.Admin;
import org.apache.commons.fileupload.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import model.dao.DongSanPhamDAO;
import model.dao.KhuyenMaiDAO;
import model.dao.LapTopDAO;
import model.dao.LoaiSanPhamDAO;
import model.dao.ManHinhDAO;
import model.dao.NhaSanXuatDAO;
import model.dao.SanPhamDAO;
import model.pojo.*;
import model.util.IntegerValueEncoder;

import org.apache.tapestry5.Block;
import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.internal.OptionModelImpl;
import org.apache.tapestry5.internal.SelectModelImpl;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.upload.services.UploadedFile;

import controller.util.ControllerHelper;

import SpaceIT.pages.Index;
import SpaceIT.pages.error;
import SpaceIT.pages.expresscheckout;

import java.io.File;


public class ThemSanPham {

	public final static int SanPhamTable =0;
		
	public final static int LaptopTable = 1;  
	  
    public final static int ManHinhTable = 2; 
    @Property
	@Persist
	private String message;
    @Property  
    private UploadedFile file;
	
	//danh sach hang san xuat
	public List<OptionModel> getAllHangSanXuat()
	{
		ArrayList<HangSanXuat> arr= NhaSanXuatDAO.LayDSHangSanXuat();
		 List<OptionModel> optionsModel = new ArrayList<OptionModel>();  
        for(int i =0;i<arr.size();i++)
        {
        	OptionModelImpl ap = new OptionModelImpl(arr.get(i).getTenHangSX(), new Integer(arr.get(i).getMaHangSX()));  
        	optionsModel.add(ap);
        }
          
        return optionsModel;  
	}
	
//	public SelectModel getDSHangSanXuat() {  
////	{        
//        return new SelectModelImpl(null, getAllHangSanXuat());  
//    } 
//  
	//hang san xuat
	@Persist
	@Validate("required")
	private Integer NSX;
	
	public Integer getNSX() {
		return NSX;
	}
	public void setNSX(Integer nSX) {
		NSX = nSX;
	}
	
	//danh sach loai san pham
	public List<OptionModel> getAllLoaiSP()
	{
		ArrayList<LoaiSanPham> arr = LoaiSanPhamDAO.LayDSLoaiSanPham();
		
		List<OptionModel> optionsModel = new ArrayList<OptionModel>();  
        for(int i =0;i<arr.size();i++)
        {
        	OptionModelImpl ap = new OptionModelImpl(arr.get(i).get_tenLoaiSanPham(), new Integer(arr.get(i).get_maLoai()));  
        	optionsModel.add(ap);
        }
          
        return optionsModel; 
	}
	public SelectModel getDSLoaiSanPham() {  
        
        return new SelectModelImpl(null, getAllLoaiSP());  
    }  
  
      
    public ValueEncoder<Integer> getValueEncoder(){  
        return new IntegerValueEncoder();  
    }  
      
	//loai san pham
	@Persist
	@Validate("required")
	private Integer LSP;
	
	public Integer  getLSP() {
		return LSP;
	}
	public void setLSP(Integer lSP) {
		LSP = lSP;
	}
	
	@Property
	private SelectModel DSHangSanXuat;
	
	
	
	@Property
	private SelectModel DSDongTheoHang;
	
		
	@InjectComponent
	private Zone dongModelZone;

	//@Inject
	//private ComponentResources componentResources;

	@Inject
	private Request request;
	
	public List<OptionModel> getAllDongSPTheoHang(int Hang)
	{
		ArrayList<DongSanPham> arr = DongSanPhamDAO.DongSanPhamTheoHangSX(Hang);
		
		List<OptionModel> optionsModel = new ArrayList<OptionModel>();  
        for(int i =0;i<arr.size();i++)
        {
        	OptionModelImpl ap = new OptionModelImpl(arr.get(i).getMoTa(), new Integer(arr.get(i).getMaDongSP()));  
        	optionsModel.add(ap);
        }
          
        return optionsModel; 
	}
	
	//set up
	void setupRender() {
		if (DSHangSanXuat == null) {
			DSHangSanXuat =new SelectModelImpl(null, getAllHangSanXuat());
			DSDongTheoHang = new SelectModelImpl(null, null);
		}	
		
	}
	
	Object onValueChangedFromHSX(int carMake1) {
		this.NSX = carMake1;
		DSHangSanXuat =new SelectModelImpl(null, getAllHangSanXuat());
		DSDongTheoHang =new SelectModelImpl(null, getAllDongSPTheoHang(this.NSX));
		
		NSX =0; //new SelectModelImpl(null, getAllDongSPTheoLoaiVaHang(this.carMake1,1));
		
		//System.out.println(this.LSP);
		return request.isXHR() ? dongModelZone.getBody() : null;
	}

	//@Persist
	@Property
	private int DSDong;
	
	
	//ma san pham
//	@Persist
//	@Property
//	@Validate("required")
//	private int MaSP;
	//ten san pham
	@Persist
	@Property
	@Validate("required")
	private String TenSP;
	//don gia
	@Persist
	@Property
	@Validate("required,min=0")
	private double DonGia;
	//danh sach Khuyen mai
	public List<OptionModel> getAllKhuyenMai()
	{
		ArrayList<KhuyenMai> arr= KhuyenMaiDAO.LayDSKhuyenMai();
		 List<OptionModel> optionsModel = new ArrayList<OptionModel>();  
        for(int i =0;i<arr.size();i++)
        {
        	OptionModelImpl ap = new OptionModelImpl(arr.get(i).getThongTinKM(), new Integer(arr.get(i).get_maKhuyenMai()));  
        	optionsModel.add(ap);
        }
          
        return optionsModel;  
	}
	public SelectModel getDSKhuyenMai() {  
        
        return new SelectModelImpl(null, getAllKhuyenMai());  
    }  
	//Khuyem mai
	@Persist
	@Validate("required")
	private int KM;
	public int getKM() {
		return KM;
	}
	public void setKM(int kM) {
		KM = kM;
	}
	//thoi gian bao hanh
	@Persist
	@Validate("required,min=0")
	private int BaoHanh;
	public int getBaoHanh() {
		return BaoHanh;
	}
	public void setBaoHanh(int baoHanh) {
		BaoHanh = baoHanh;
	}
	//số lượng tồn
	@Persist
	@Property
	@Validate("required,min=0")
	private int SoLuong;
	//tinh trang
	@Persist
	@Property
	@Validate("required")
	private String TinhTrang;
	//hien trang
	@Persist
	@Property
	@Validate("required")
	private String HienTrang;
	//thong tin them
	@Validate("required")	
	@Property  
    @Persist(PersistenceConstants.FLASH)  
    private Integer ThongTinThem;  

	public void beginRender() {  
        if (ThongTinThem == null) {  
        	ThongTinThem = SanPhamTable;  
        }  
    } 
	
	//get laptop
	public Integer getLaptop()
	{
		return LaptopTable;
	}
	//show thong tin laptop
	public Boolean getshowThongTinLaptop()
	{
		Boolean value = (ThongTinThem == LaptopTable ? true : false);  
        return value;  
	}
	//get man hinh
	public Integer getManHinh()
	{
		return ManHinhTable;
	}
	//show thong tin Man hinh
		public Boolean getshowThongTinManHinh()
		{
			Boolean value = (ThongTinThem == ManHinhTable ? true : false);  
	        return value;  
		}
	//khac
	public Integer getKhac()
	{
		return SanPhamTable;
	}
	//show thong tin Khác
	public Boolean getshowThongTinKhac()
	{
		Boolean value = (ThongTinThem == SanPhamTable ? true : false);  
        return value;  
	}
	
	//mo ta khac
	@Persist
	@Property
	@Validate("required")
	private String MoTaKhac;
	
	//San pham lap top
	@Persist
	@Property
	@Validate("required")
	private String  Microprocessor;
	
	@Persist
	@Property
	@Validate("required,min=0")
	private float TocDoCPU;
	
	@Persist
	@Property
	@Validate("required")
	private String Cache;
	
	@Persist
	@Property
	@Validate("required,min=0")
	private float DungLuongRAM;
	
	@Persist
	@Property
	@Validate("required")
	private String ThongTinRAM;
	
	@Persist
	@Property
	@Validate("required")
	private String Graphic;
	
	@Persist
	@Property
	@Validate("required,min=0")
	private float HDD;
	
	@Persist
	@Property
	@Validate("required")
	private String ThongTinHDD;
	
	@Persist
	@Property
	@Validate("required")
	private String Multimedia;
	
	@Persist
	@Property
	@Validate("required")
	private String Integrated;
	
	@Persist
	@Property
	@Validate("required")
	private String Display;
	
	@Persist
	@Property
	@Validate("required")
	private String NetworkCard;
	
	@Persist
	@Property
	@Validate("required")
	private String Wireless;
	
	@Persist
	@Property
	@Validate("required")
	private String Keyboard;
	
	@Persist
	@Property
	@Validate("required")
	private String Pointing;
	
	@Persist
	@Property
	@Validate("required")
	private String External;
	
	@Persist
	@Property
	@Validate("required,min=0")
	private float Weight;
	@Persist
	@Property
	@Validate("required,min=0")
	private int  Pin;
	@Persist
	@Property
	@Validate("required")
	private String Soft;
	
	@Persist
	@Property
	@Validate("required")
	private String MoTaLaptop;
	
	//Màn hình
	@Persist
	@Property
	@Validate("required")
	private String Model;
	
	@Persist
	@Property
	@Validate("required")
	private String LoaiManHinh;
	
	@Persist
	@Property
	@Validate("required")
	private String KichThuocDiemAnh;
	
	@Persist
	@Property
	@Validate("required")
	private String CuongDoSang;
	
	@Persist
	@Property
	@Validate("required")
	private String DoTuongPhan;
	
	@Persist
	@Property
	@Validate("required")
	private String GocNhin;
	
	@Persist
	@Property
	@Validate("required")
	private String DoPhanGiai;
	
	@Persist
	@Property
	@Validate("required")
	private String KhaNangHienThiMau;
	
	@Persist
	@Property
	@Validate("required")
	private String ThoiGianDapUng;
	
	@Persist
	@Property
	@Validate("required")
	private String KetNoiHoTro;
	
	@Persist
	@Property
	@Validate("required")
	private String HieuDienTheVao;
	
	@Persist
	@Property
	@Validate("required")
	private String CongSuatTieuThu;
	
	@Persist
	@Property
	@Validate("required,min=0")
	private float TrongLuong;
	
	@Persist
	@Property
	@Validate("required")
	private String KichThuoc;
	
	@Persist
	@Property
	@Validate("required")
	private String MoTaMH;
	
	//phuong thuc
	private String taoMoTaManHinh()
	{
		String kq ="";
		if(this.KichThuoc.isEmpty() ==false)
		{
			kq += " Kích Thước: " + this.KichThuoc;
		}
		if(this.DoTuongPhan.isEmpty()==false)
		{
			kq += " Độ tương Phản: " + this.DoTuongPhan;
		}
		if(this.DoPhanGiai.isEmpty()==false)
		{
			kq += " Độ phân giải tối đa: " + this.DoPhanGiai;
		}
		if(this.ThoiGianDapUng.isEmpty()==false)
		{
			kq += " Thời gian đáp ứng: " + this.ThoiGianDapUng;
		}
		if(this.KichThuocDiemAnh.isEmpty()==false)
		{
			kq += " Kích thước điểm ảnh: " + this.KichThuocDiemAnh;
		}
		if(this.CuongDoSang.isEmpty()==false)
		{
			kq += " Cường độ Sáng: " +this.CuongDoSang;
		}
		if(this.GocNhin.isEmpty()==false)
		{
			kq += " Góc nhìn: " + this.GocNhin;
		}
		if(this.KhaNangHienThiMau.isEmpty()==false)
		{
			kq += " Khả năng hiển thị màu: " + this.KhaNangHienThiMau;
		}
		if(this.MoTaMH.isEmpty()==false)
		{
			kq += " " + this.MoTaMH;
		}
		return kq;
	}
	//phuong thuc
	private String taoMoTaLapTop()
	{
		String kq = "";
		if(this.Graphic.isEmpty()==false)
		{
			kq +=", " + this.Graphic;
		}
		if(this.Multimedia.isEmpty()==false)
		{
			kq +=", "+ this.Multimedia;
		}
		if(this.Integrated.isEmpty()==false)
		{
			kq +=", " + this.Integrated;
		}
		if(this.Display.isEmpty()==false)
		{
			kq +=", "+ this.Display;
		}
		if(this.NetworkCard.isEmpty()==false)
		{
			kq+=", " + this.NetworkCard;
		}
		if(this.Wireless.isEmpty()==false)
		{
			kq +=", "+ this.Wireless;
		}
		if(this.External.isEmpty()==false)
		{
			kq += ", " + this.External;
		}
		if(this.Soft.isEmpty()==false)
		{
			kq +=", " + this.Soft;
		}
		return kq;
	}
	//tao Laptop
	private LapTop taoLaptop()
	{
		LapTop sp = new LapTop();
		sp.setTenSP(ControllerHelper.filter(this.TenSP));
		sp.setLoaiSP(this.LSP);
		//sp.setMoTaSP(moTaSP)
		sp.setGiaBan(this.DonGia);
		sp.setKhuyenMai(this.KM);
		sp.setThoiGianBaoHanh(this.BaoHanh);
		sp.setHangSanXuat(this.NSX);
		sp.setSoLuongTon(this.SoLuong);
		sp.setTinhTrang(ControllerHelper.filter(this.TinhTrang));
		sp.setHienTrang(ControllerHelper.filter(this.HienTrang));
		sp.setSoLuotXem(0);
		sp.setMaDongSP(this.DSDong);
		
		sp.setMicroProcessor(ControllerHelper.filter(this.Microprocessor) + " " + this.TocDoCPU+"GHZ");
		sp.setCache(ControllerHelper.filter(this.Cache));
		sp.setMemory(ControllerHelper.filter(this.ThongTinRAM) +" " + this.DungLuongRAM + "GB");
		sp.setVideoGraphic(ControllerHelper.filter(this.Graphic));
		sp.setHDD(ControllerHelper.filter(this.ThongTinHDD) +" "+ this.HDD +"GB");
		sp.setMultimediaDrive(ControllerHelper.filter(this.Multimedia));
		sp.setIntegrated(ControllerHelper.filter(this.Integrated));
		sp.setDisplay(ControllerHelper.filter(this.Display));
		sp.setNetworkCard(ControllerHelper.filter(this.NetworkCard));
		sp.setWirelessConnectivity(ControllerHelper.filter(this.Wireless));
		sp.setKeyboard(ControllerHelper.filter(this.Keyboard));
		sp.setPointingDevice(ControllerHelper.filter(this.Pointing));
		sp.setExternalPorts(ControllerHelper.filter(this.External));
		sp.setWeight(this.Weight);
		sp.setSoft(ControllerHelper.filter(this.Soft));
		sp.setTocDoCPU(this.TocDoCPU);
		sp.setDungLuongRAM(this.DungLuongRAM);
		sp.setDungLuongHDD(this.HDD);
		sp.setSoLuongCell(this.Pin);
		return sp;
	}
	//tao man hinh
	private ManHinhPOJO taoManHinh()
	{
		ManHinhPOJO sp = new ManHinhPOJO();
		
		sp.setTenSP(ControllerHelper.filter(this.TenSP));
		sp.setLoaiSP(this.LSP);
		//sp.setMoTaSP(moTaSP)
		sp.setGiaBan(this.DonGia);
		sp.setKhuyenMai(this.KM);
		sp.setThoiGianBaoHanh(this.BaoHanh);
		sp.setHangSanXuat(this.NSX);
		sp.setSoLuongTon(this.SoLuong);
		sp.setTinhTrang(ControllerHelper.filter(this.TinhTrang));
		sp.setHienTrang(ControllerHelper.filter(this.HienTrang));
		sp.setSoLuotXem(0);
		sp.setMaDongSP(this.DSDong);
		
		sp.setModel(ControllerHelper.filter(this.Model));
		
		sp.setLoaiManHinh(ControllerHelper.filter(this.LoaiManHinh));
		sp.setKichThuocDiemAnh(ControllerHelper.filter(this.KichThuocDiemAnh));
		sp.setCuongDoSang(ControllerHelper.filter(this.CuongDoSang));
		sp.setDoTuongPhan(ControllerHelper.filter(this.DoTuongPhan));
		sp.setGocNhin(ControllerHelper.filter(this.GocNhin));
		sp.setDoPhanGiai(ControllerHelper.filter(this.DoPhanGiai));
		sp.setKhaNangHienThiMau(ControllerHelper.filter(this.KhaNangHienThiMau));
		sp.setThoiGianDapUng(ControllerHelper.filter(this.ThoiGianDapUng));
		sp.setKetNoiHoTro(ControllerHelper.filter(this.KetNoiHoTro));
		sp.setHieuDienTheVao(ControllerHelper.filter(this.HieuDienTheVao));
		sp.setCongSuatTieuThu(ControllerHelper.filter(this.CongSuatTieuThu));
		sp.setTrongLuong(this.TrongLuong);
		sp.setKichThuoc(ControllerHelper.filter(this.KichThuoc));
		
		return sp;
	}
	//tap san pham khac
	private SanPhamKhac taoSanPham()
	{
		
		SanPhamKhac sp = new SanPhamKhac();
		
		sp.setTenSP(ControllerHelper.filter(this.TenSP));
		sp.setLoaiSP(this.LSP);
		//sp.setMoTaSP(moTaSP)
		sp.setGiaBan(this.DonGia);
		sp.setKhuyenMai(this.KM);
		sp.setThoiGianBaoHanh(this.BaoHanh);
		sp.setHangSanXuat(this.NSX);
		sp.setSoLuongTon(this.SoLuong);
		sp.setTinhTrang(ControllerHelper.filter(this.TinhTrang));
		sp.setHienTrang(ControllerHelper.filter(this.HienTrang));
		sp.setSoLuotXem(0);
		sp.setMaDongSP(this.DSDong);
	
		return sp;
	}
	
	public String EmptyMessage(){
		message = "";
		return "";
	}
	
	@OnEvent(value="submit", component="formThemSanPham")
	void onFormSubmit() 
	{	
		//kq = "";
		
		try
        {
			if(SanPhamDAO.KiemTraSanPham(ControllerHelper.filter(this.TenSP))==true)
			{
				message = "Sản Phẩm Này Đã Tồn Tại";
				return;
			}
    		  String Path = this.getClass().getResource("./../../../").toURI().getRawPath();
    		  Path = Path.substring(0,Path.lastIndexOf("/") - 1);
    		  Path = Path.substring(0,Path.lastIndexOf("/") -1);
    		  Path = Path.substring(0,Path.lastIndexOf("/"));
    		  Path += "/src/main/webapp/layout/images/SanPham/";      		  
    		 
    		  
		      File copied = new File(Path + file.getFileName());	
		     
		      file.write(copied);
		      if(copied.exists())
		      {
		    	 
		    	  String link =  "layout//images//SanPham//"+ file.getFileName(); 
		    	  if(ThongTinThem ==ManHinhTable)
		    	  {
		    		  ManHinhPOJO sp = taoManHinh();
		    		  
		    		  String moTa="";
		    		  moTa += this.TenSP;
		    		  moTa +=" Model:"+ this.Model;		    		  
		    		  moTa +=" Loại màn hình: "+ this.LoaiManHinh;
		    		  
		    		  moTa += taoMoTaManHinh();		    		 
		    		 
		    		  moTa += ", "+ this.BaoHanh;
		    		  moTa +=", "+ MoTaMH;		    		  
		    		 
		    		  sp.setMoTaSP(ControllerHelper.filter(moTa));
		    		  sp.setLinkHinhAnh(link);
		    		  //insert
		    		  boolean kqIn = ManHinhDAO.ThemSanPhamManHinh(sp);
		    		  if(kqIn==true)
		    		  {
		    			  
		    			  //System.out.print("Thêm Man Hinh TC");
		    			  message = "Thêm Màn Hình thành Công";
		    		  }
		    		  else
		    			  message = "Thêm Màn Hình Thất Bại";
		    	  }
		    	  else if(ThongTinThem == LaptopTable)
		    	  {
		    		  LapTop sp = taoLaptop();		    		  
		    		  
		    		  String mota ="";
		    		  mota += this.TenSP;
		    		  mota +=", " +this.Microprocessor;
		    		  mota +=", "+ String.valueOf(this.TocDoCPU) + "GHz";
		    		  mota +=", " +String.valueOf(this.DungLuongRAM) +"GB";
		    		  if(this.ThongTinRAM.isEmpty()==false)
		    		  {
		    			  mota += ", " + this.ThongTinRAM;
		    		  }
		    		  mota +=", " +String.valueOf(this.HDD) +"GB";
		    		  if(this.ThongTinHDD.isEmpty()==false)
		    		  {
		    			  mota += ", " + this.ThongTinHDD;
		    		  }
		    		  mota += taoMoTaLapTop();
		    		  mota +=", "+ this.Pin +" cell";
		    		  mota += ", "+ this.BaoHanh;
		    		  mota +=", " + this.MoTaLaptop;
		    		  
		    		  sp.setMoTaSP(ControllerHelper.filter(mota));
		    		  sp.setLinkHinhAnh(link);
		    		  //insert
		    		  boolean kqIn = LapTopDAO.ThemSanPhamLaptop(sp);
		    		  
		    		  if(kqIn==true)
		    		  {
		    			  
		    			  message ="Thêm Laptop Thành Công";
		    		  }
		    		  else
		    			  message ="Thêm Laptop Thất Bại";
		    	  }
		    	  else
		    	  {
		    		  SanPham sp = taoSanPham();
		    		 
		    		  String moTa="";		    		  
		    		  moTa += this.TenSP;
		    		  moTa += ", "+ this.BaoHanh;
		    		  moTa += this.MoTaKhac;
		    		  
		    		  sp.setMoTaSP(ControllerHelper.filter(moTa));
		    		 // System.out.print("link: "+link);
		    		  sp.setLinkHinhAnh(link);
		    		  //insert
		    		  boolean kqIn = SanPhamDAO.ThemSanPham(sp);
		    		  if(kqIn==true)
		    		  {
		    			 message ="Thêm Sản Phẩm Thành Công";
		    			 //System.out.print("Thêm SP TC");
		    		  }
		    		  else
		    			  message ="Thêm Sản Phẩm Thất Bại";
		    	  }
		    	  //return expresscheckout.class;
	      }
		     else
		    	 message="Lỗi upload Hình Ảnh";
		    	  
		     
      	}
    	    catch (Exception e) {
			// TODO: handle exception
    	    	message="Lỗi Thêm Sản Phẩm";
    	    	//System.out.print("Loi: "+ e.getMessage());
		}
		//return ketQua;
	}
	@ApplicationState
    private NhanVien NV;
	public Object onActivate()
	{
		if(NV.isDangNhap()==false)
		{			
			//NV.setDangNhap(false);
			return Index.class;
		}
		return null;
	}
}
